package com.atguigu.srb.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.exception.GlobalException;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.utlis.SmsProperties;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Override
    public void send(String mobile, String templateCode, Map<String, Object> param) {

        //创建远程链接客户端对象
        DefaultProfile profile = DefaultProfile.getProfile(
                SmsProperties.REGION_Id,
                SmsProperties.KEY_ID,
                SmsProperties.KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        //创建远程链接的各项请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", SmsProperties.REGION_Id);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", SmsProperties.SIGN_NAME);
        request.putQueryParameter("TemplateCode", templateCode);

//        TemplateParam需要json格式，所以把map转json
        Gson gson = new Gson();
        String jsonParam = gson.toJson(param);
        request.putQueryParameter("TemplateParam", jsonParam);
        try {
//            使用客户端对象，携带请求参数向阿里云服务器发起远程调用，并得到响应结果
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("response.getData():" + response.getData());

            //通讯失败的处理，阿里云短信服务响应失败
            boolean success = response.getHttpResponse().isSuccess();
            Assert.isTrue(success,ResponseEnum.ALIYUN_RESPONSE_ERROR);

            //获取响应结果
            String data = response.getData();
//            json转map
            HashMap<String,String> resultMap = gson.fromJson(data, HashMap.class);
            String code = resultMap.get("Code");
            String message = resultMap.get("Message");
            log.info("code"+ code + ",message" + message);

            //业务失败的处理，给前端提示
            Assert.equals("OK",code,ResponseEnum.ALIYUN_SMS_ERROR);

        } catch (ServerException e) {
               //记录日志
               log.error("阿里云短信发送sdk调用失败："+ e.getErrCode()+","+e.getErrMsg());
               //返回给前端
               throw new GlobalException(ResponseEnum.ALIYUN_SMS_ERROR,e);
//            e.printStackTrace();
        } catch (ClientException e) {
            //记录日志
            log.error("阿里云短信发送sdk调用失败："+ e.getErrCode()+","+e.getErrMsg());
            //返回给前端
            throw new GlobalException(ResponseEnum.ALIYUN_SMS_ERROR,e);
        }
    }
}
